package com.ruoyi.web.controller.interview;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interview.domain.InterviewDimension;
import com.ruoyi.interview.domain.InterviewPosition;
import com.ruoyi.interview.domain.InterviewQuestion;
import com.ruoyi.interview.service.*;
import com.ruoyi.web.util.ChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangjie
 * @date 2025/11/11
 */
@RestController
@RequestMapping("/interview/question")
public class InterviewQuestionController {

    @Autowired
    private IInterviewPositionService interviewPositionService;

    @Autowired
    private IInterviewDimensionService interviewDimensionService;
    //面试记录
    @Autowired
    private IInterviewRecordService interviewRecordService;

    //面试问题数量
    private static final int QUESTION_NUMBER = 5;
    @Autowired
    private IInterviewQuestionService iInterviewQuestionService;

    @GetMapping("/genQuestion")
    public AjaxResult genQuestion(Long positionId) {
        //1.根据id查询岗位信息
        InterviewPosition interviewPosition = interviewPositionService.selectInterviewPositionById(positionId);
        //2.查询岗位id对应的维度信息
        List<InterviewDimension> dimensions=interviewDimensionService.getDemensionListByPositionId(positionId);
        String s = genDimensionPrompt(dimensions);

        //3.根据岗位名称生成模拟面试的问题
        IQuestionGenerateService service = ChatUtil.getService(IQuestionGenerateService.class);

        String message = service.generateQuestionByPosition(interviewPosition.getName(), QUESTION_NUMBER,s);
        //4.生成一次面试记录，解析这次记录的问题，保存到数据库中,返回面试记录id   转成json字符串输出
        Long recordId=interviewRecordService.generateRecord(positionId,message);

        return AjaxResult.success(recordId);
    }

    @GetMapping("/getQuestions")
    public AjaxResult getQuestions(Long recordId) {
        InterviewQuestion interviewQuestion = new InterviewQuestion();
        interviewQuestion.setRecordId(recordId);
        List<InterviewQuestion> questions = iInterviewQuestionService.selectInterviewQuestionList(interviewQuestion);
        return AjaxResult.success(questions);
    }


    /**
     * json化维度数据
     * @param
     * @return
     */
    private String genDimensionPrompt(List<InterviewDimension> dimensionList) {
        return dimensionList.stream().map(dimension -> {
            return "[" + dimension.getName() + "("+ dimension.getWeight() +")]";
        }).collect(Collectors.joining("、"));
    }


}
